package com.bangcommunity.bbframe.http.interceptor;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * @author tanghc
 * @date 18/1/17
 */
public class RestInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request originalRequest = chain.request();
        String cacheControl = originalRequest.cacheControl().toString();

        Request.Builder requestBuilder = originalRequest.newBuilder()
                // Basic Authentication,也可用于token验证,OAuth验证
                // .header("Authorization", basic)
                .header("Accept", "application/json").method(originalRequest.method(), originalRequest.body());

        Request request = requestBuilder.build();

        Response originalResponse = chain.proceed(request);
        Response.Builder responseBuilder =
                // Cache control设置缓存
                originalResponse.newBuilder().header("Cache-Control", cacheControl);

        return responseBuilder.build();
    }
}